﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.Algorithm;
using Microhard.Core.DBUtility;

namespace Microhard.Permission.MVCFilter
{
    public class BeforeDeleteSingleModelActionFilter : IActionDataFilter
    {
        #region IActionDataFilter Members

        public bool FilterAction(System.Web.Mvc.ActionExecutingContext filterContext, Models.User currentUser, List<Models.Permission> currentPermissions, Core.DBUtility.ScopeResult dataScope)
        {
            var models = filterContext.ActionParameters["models"] as List<object>;
            List<object> del = new List<object>();
            foreach (var m in models)
            {
                Guid id = AlgCompare.GetPKID<ModelPKIDAttribute>(m);
                this.PKID = id;
                if (dataScope.PKID.Contains(id))
                {
                    del.Add(m);
                }
                var delbpm = m as BasePermissionModel;
                if (delbpm != null)
                {
                    delbpm.OperationStatus = OperationStatus.Delete;
                }
            }
            filterContext.ActionParameters["models"] = del;
            return true;
        }

        public Guid PKID
        {
            set;
            get;
        }
        #endregion
    }
}
